const axios = require('axios');

// 配置axios
const api = axios.create({
  baseURL: 'http://localhost:8080',
  timeout: 10000,
  withCredentials: true
});

async function testLoginWithoutCaptcha() {
  console.log('=== 测试无验证码登录 ===');
  
  try {
    // 1. 测试登录（不需要验证码）
    console.log('1. 尝试登录...');
    const loginResponse = await api.post('/login', {
      username: 'admin',
      password: 'admin123',
      // 不再需要code和uuid参数
      code: '',
      uuid: ''
    });
    
    console.log('登录响应:', loginResponse.data);
    
    // 2. 获取token
    const token = loginResponse.headers['authorization'];
    console.log('获取到的token:', token);
    
    // 3. 测试学生信息API
    console.log('\\n2. 测试学生信息API...');
    const studentResponse = await api.get('/system/student/list', {
      headers: {
        'Authorization': token
      },
      params: {
        pageNum: 1,
        pageSize: 10
      }
    });
    
    console.log('学生信息API响应:', studentResponse.data);
    console.log('学生数量:', studentResponse.data.total);
    
    if (studentResponse.data.rows && studentResponse.data.rows.length > 0) {
      console.log('学生列表:');
      studentResponse.data.rows.forEach((student, index) => {
        console.log(`  ${index + 1}. ${student.studentName} - ${student.description}`);
      });
    }
    
  } catch (error) {
    console.error('测试失败:', error.response ? error.response.data : error.message);
    
    if (error.response && error.response.status === 401) {
      console.log('认证失败，可能是token无效或未登录');
    }
  }
}

// 运行测试
testLoginWithoutCaptcha();